package model

import (
	//"github.com/jinzhu/gorm"
	//_ "github.com/jinzhu/gorm/dialects/mysql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"log"
	"time"
)

type  Orders struct {
	OrderId int32 `xorm:"pk autoincr"`
	CustomerId int32
	//CustomerName string
	AdviserId int32
	OrderType int
	ShipmentStatus int
	PaymentStatus int
	OrderStatus int
	BillingStatus int
	OrderDate time.Time
}

var x *xorm.Engine

func init() {
	var xErr error
	x, xErr = xorm.NewEngine("mysql", "root:new_password@tcp(127.0.0.1:3306)/combined?charset=utf8")
	if xErr != nil {
		log.Fatal("数据库连接失败:", xErr)
	}
	if err := x.Sync(new(Orders), new(Advisers), new(Departments), new(Members), new(MeiyeOrders), new(PaymentMethods), new(MeiyeOrderPayment), new(CallLog)); err != nil {
		log.Fatal("数据表同步失败:", err)
	}
}

func NewOrder() *Orders{
	return new(Orders)
}

//var (
//	db *gorm.DB
//	err error
//)
//
//func init(){
//	db, err = gorm.Open("mysql", "root:new_password@tcp(localhost:3306)/gorm_combined?charset=utf8&parseTime=true")
//	if err != nil {
//		panic("failed to connect database")
//	}
//	db.AutoMigrate(&Orders{}, &Advisers{}, &Departments{})
//}


func (o *Orders)CombinedQuery(qo *QueryOrder)([]*Orders, int, error){
	var orderList []*Orders

	var session = x.NewSession()

	if qo.OrderId != ""{
		session = session.Where("order_id = ?", qo.OrderId )
	}

	if qo.CustomerId != ""{
		session = session.And("customer_id = ?", qo.CustomerId )
			//Or("customer_name like ?", "%"+qo.CustomerId).
			//Or("customer_name like ?", "%"+qo.CustomerId+"%").
			//Or("customer_name like ?", qo.CustomerId+"%")
	}

	if qo.DepartmentId != ""{
		session = session.Join("INNER ", "advisers", "orders.adviser_id = advisers.id" ).
			Join("INNER", "departments", "advisers.department_id = departments.id").
			Where("advisers.department_id = ?", qo.DepartmentId)
	}

	if qo.OrderType != ""{
		session = session.And("order_type = ?", qo.OrderType )
	}

	if qo.ShipmentStatus != ""{
		session = session.And("shipment_status = ?", qo.ShipmentStatus )
	}

	if qo.PaymentStatus != ""{
		session = session.And("payment_status = ?", qo.PaymentStatus )
	}

	if qo.OrderStatus != ""{
		session = session.And("order_status = ?", qo.OrderStatus )
	}

	if qo.BillingStatus != ""{
		session = session.And("billing_status = ?", qo.BillingStatus )
	}

	if qo.OrderStartDate != ""{
		session = session.And("order_date >= ?", qo.OrderStartDate )
	}

	if qo.OrderEndDate != ""{
		session = session.And("order_date <= ?", qo.OrderEndDate )
	}

	//err := session.OrderBy("order_date desc").
	//	Find(&orderList)
	//if err != nil{
	//	return nil, 0, err
	//}

	err := session.OrderBy("order_date desc").Limit(qo.PageSize, (qo.PageNo-1)*qo.PageSize).
		Find(&orderList)
	if err != nil{
		return nil, 0, err
	}

	totals, tErr := session.Count(new(Orders))
	if tErr != nil{
		return nil, 0, tErr
	}

	return orderList,int(totals),  nil

	//totals := len(orderList)
	//
	//if totals < qo.PageSize*qo.PageNo && totals > (qo.PageNo-1)*qo.PageSize{
	//	return orderList[(qo.PageNo-1)*qo.PageSize:], totals, nil
	//}
	//
	//if totals <= (qo.PageNo-1)*qo.PageSize{
	//	return nil, 0, nil
	//}
	//
	//return orderList[(qo.PageNo-1)*qo.PageSize:qo.PageNo*qo.PageSize], totals, nil
}


//func (o *Orders)CombinedQuery(qo *QueryOrder)([]*Orders, int, error){
//	var orderList []*Orders
//
//	var gormSession = db.New()
//
//	if qo.OrderId != ""{
//		gormSession = gormSession.Where("order_id = ?", qo.OrderId )
//	}
//
//	if qo.CustomerId != ""{
//		gormSession = gormSession.Where("customer_id = ?", qo.CustomerId )
//	}
//
//	if qo.DepartmentId != ""{
//		gormSession = gormSession.Joins("JOIN advisers ON orders.adviser_id = advisers.id").
//			Joins("JOIN departments ON advisers.department_id = departments.id AND advisers.department_id = ?", qo.DepartmentId)
//	}
//
//	if qo.OrderType != ""{
//		gormSession = gormSession.Where("order_type = ?", qo.OrderType )
//	}
//
//	if qo.ShipmentStatus != ""{
//		gormSession = gormSession.Where("shipment_status = ?", qo.ShipmentStatus )
//	}
//
//	if qo.PaymentStatus != ""{
//		gormSession = gormSession.Where("payment_status = ?", qo.PaymentStatus )
//	}
//
//	if qo.OrderStatus != ""{
//		gormSession = gormSession.Where("order_status = ?", qo.OrderStatus )
//	}
//
//	if qo.BillingStatus != ""{
//		gormSession = gormSession.Where("billing_status = ?", qo.BillingStatus )
//	}
//
//	if qo.OrderStartDate != ""{
//		gormSession = gormSession.Where("order_date >= ?", qo.OrderStartDate )
//	}
//
//	if qo.OrderEndDate != ""{
//		gormSession = gormSession.Where("order_date <= ?", qo.OrderEndDate )
//	}
//
// 	//分页
//	gormSession.Order("order_date desc").Limit(qo.PageSize).Offset((qo.PageNo - 1)* qo.PageSize).Find(&orderList)
//
//	//gormSession.Order("order_date desc").Find(&orderList)
//
//	totals := len(orderList)
//
//	return orderList, totals, nil
//
//	//if totals < qo.PageSize*qo.PageNo && totals > (qo.PageNo-1)*qo.PageSize{
//	//	return orderList[(qo.PageNo-1)*qo.PageSize:], totals, nil
//	//}
//	//
//	//if totals <= (qo.PageNo-1)*qo.PageSize{
//	//	return nil, 0, nil
//	//}
//
//	//return orderList[(qo.PageNo-1)*qo.PageSize:qo.PageNo*qo.PageSize], totals, nil
//}